home *** CD-ROM | disk | FTP | other *** search
- #include <SetupA4.h>
- #include "ExternalInterface.h"
-
- // Insert Column by Matthew Xavier Mora
- // 3-05-93
- // Do with it what you will. But be gentle :-)
- // Since you have the source you can correct the bugs yourselves.
-
- pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w)
- {
- long line_count;
- long line;
- long line_start;
- long line_end;
- long selStart,selEnd,firstChar,columnStart;
- Handle text;
- char tabChar = 0x09;
- char cr = 0x0D;
- GrafPtr save_port;
- long oldLength,actualLength;
- Handle newtext;
- EventRecord theEvent;
- long selLine;
-
- RememberA0();
- SetUpA4();
-
- if (w == nil)
- goto done;
- GetPort(&save_port);
- // Hold down the cmd key for help
- EventAvail(everyEvent,&theEvent);
- if ( theEvent.modifiers & cmdKey ) {
- Alert(128,nil);
- SetPort(save_port);
- goto done;
- }
-
-
- line_count = callbacks->GetLastLine();
- text = callbacks->GetWindowContents(w);
- callbacks->GetSelection(&selStart,&selEnd,&firstChar);
- line_start = callbacks->GetLineStart(selStart);
- columnStart = selStart - line_start;
- selStart = columnStart;
- callbacks->SetSelection(selStart,selStart,firstChar);
-
-
- oldLength = GetHandleSize(text);
-
- newtext = callbacks->Allocate(oldLength+line_count,true);
-
- if (newtext == nil) {
- callbacks->StartProgress("\pInserting Column (slow)…", line_count , FALSE);
- for (line = 0; line < line_count; line++)
- {
- if (callbacks->DoProgress(line)) line = line_count;
- callbacks->Insert(&tabChar, 1L);
- line_end = callbacks->GetLineEnd(selStart);
- selStart = (++line_end) + columnStart;
- callbacks->SetSelection(selStart,selStart,firstChar);
- }
- } else {
- register char *textptr; //ptr to old text
- register char *newtextptr; //ptr to new text
- register long charcount = 1; //character count of where we are on a line
- char *endChar; //ending char
-
- HLock(newtext); //lock 'em just in case
- HLock(text);
-
- newtextptr = *newtext;
- textptr = *text;
- endChar = textptr + oldLength;
- callbacks->StartProgress("\pInserting Column (fast)…", (long)endChar , FALSE);
-
- if (columnStart == 0) //kludge for first line
- *(newtextptr++) = tabChar; //It didn't insert a tab if the col started at zero
-
- for (; textptr < endChar;textptr++) {
- if (*textptr == cr){
- charcount = 0;
- callbacks->DoProgress((long)textptr);
- }
- *(newtextptr++) = *textptr;
- if (charcount++ == columnStart)
- *(newtextptr++) = tabChar;
-
- }
-
- HUnlock(newtext);
- HUnlock(text);
- callbacks->SetWindowContents(w,newtext);
- callbacks->SetSelection(0,0,0);
- callbacks->DoneProgress();
-
- }
- done:
- RestoreA4();
- }
-